package ywpt.web.action.client;

import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.DeviceParts;
import ywpt.service.client.DevicePartsService;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class DevicePartsAction extends BaseAction implements
ModelDriven<DeviceParts>, Preparable{

	/**
	 * 设备部件Action
	 */
	private static final long serialVersionUID = -1636827805663767631L;
	private Long id;
	private DeviceParts deviceParts;
	private DevicePartsService devicePartsService;
	private Long deviceId;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public DeviceParts getDeviceParts() {
		return deviceParts;
	}

	public void setDeviceParts(DeviceParts deviceParts) {
		this.deviceParts = deviceParts;
	}

	public DevicePartsService getDevicePartsService() {
		return devicePartsService;
	}

	public void setDevicePartsService(DevicePartsService devicePartsService) {
		this.devicePartsService = devicePartsService;
	}

	public Long getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(Long deviceId) {
		this.deviceId = deviceId;
	}
//*****************************************************************************
	@Override
	public DeviceParts getModel() {
		return this.deviceParts;
	}

	@Override
	public void prepare() throws Exception {
		if( id!= null){
			this.deviceParts = this.devicePartsService.getDevicePartsById(id);
		}else{
			this.deviceParts = new DeviceParts();
		}
	}
	//*****************************************************************************
	public String input(){	
		String code = this.devicePartsService.CreateCode(deviceId);
		deviceParts.setCode(code);
		return INPUT;
	} 

	public String edit(){
		this.deviceParts = devicePartsService.getDevicePartsById(id);
		if(deviceParts.getDevice()!= null){
			this.setDeviceId(deviceParts.getDevice().getId());
		}
		return INPUT;
	}
	/**
	 * ����
	 * 
	 * @return
	 */
	public String save() {
		try {
			if (id == null) {
				devicePartsService.saveDeviceParts(deviceParts ,deviceId);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
			} else {
				devicePartsService.updateDeviceParts(deviceParts,deviceId);
				this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
			}
			return SUCCESS;
		} catch (UniqueConstraintException e) {
			this.addActionError(SystemMessage.SAVE_FAIL + e.getMessage());
			return INPUT;
		}
	}
	 public String delete() throws Exception {
		if (id != null) 
		{	
			devicePartsService.removeDeviceParts(id);
			return SUCCESS;				
		}else {
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
	}
	
}
